package com.example.andylau96.res.Main;

import android.app.Fragment;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.example.andylau96.res.R;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;

/**
 * Created by AndyLau96 on 2016/11/22.
 */
public class changeFragment extends Fragment implements View.OnClickListener,AdapterView.OnItemClickListener
{
    private ListView listview;
    private ImageButton re;
    private Button ok;
    private int choice=-1;
    private String theme;
    private LinearLayout title;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.change_theme,container,false);
        listview=(ListView) (view.findViewById(R.id.change_theme_list));
        SimpleAdapter adapter= MainActivity.getAdapter();
        listview.setAdapter(adapter);
        listview.setOnItemClickListener(this);
        re=(ImageButton) (view.findViewById(R.id.change_title_return));
        ok=(Button)(view.findViewById(R.id.change_title_ok));
        re.setOnClickListener(this);
        ok.setOnClickListener(this);
        title=(LinearLayout) (view.findViewById(R.id.theme_title));
        return view;
    }
    @Override
    public void onClick(View view) {
        int id=view.getId();
        if(id==R.id.change_title_return)
        {
            getFragmentManager().popBackStack();
        }
        if(id==R.id.change_title_ok) {
            if (choice == -1)
                Toast.makeText(getActivity(), "请选择主题后再保存", Toast.LENGTH_SHORT).show();
            else {
                switch (choice) {
                    case 0:
                        theme = "#FF0000";
                        break;
                    case 1:
                        theme = "#FFAEB9";
                        break;
                    case 2:
                        theme = "#f6581d";
                        break;
                    case 3:
                        theme = "#6aacf2";
                        break;
                    case 4:
                        theme = "#98768a";
                        break;
                    case 5:
                        theme = "#e39917";
                        break;
                    case 6:
                        theme = "#3bc331";
                        break;
                    case 7:
                        theme = "#12b7f5";
                        break;
                }
                try {
                    FileOutputStream crate = getActivity().openFileOutput("color.txt", getActivity().MODE_PRIVATE);
                    crate.write(theme.getBytes());
                    crate.close();
                    Toast.makeText(getActivity(), "保存成功!", Toast.LENGTH_SHORT).show();
                } catch (Exception e) {
                    Toast.makeText(getActivity(), "保存失败!", Toast.LENGTH_SHORT).show();
                    e.printStackTrace();
                }
                getFragmentManager().popBackStack();
            }
        }
        }


    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        ImageView iv=null;
        choice=i;
        switch (i)
        {
            case 0:
                iv= (ImageView) view.findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_1);
                iv= (ImageView) adapterView.getChildAt(1).findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_write);
                iv= (ImageView) adapterView.getChildAt(2).findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_write);
                iv= (ImageView) adapterView.getChildAt(3).findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_write);
                iv= (ImageView) adapterView.getChildAt(4).findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_write);
                iv= (ImageView) adapterView.getChildAt(5).findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_write);
                iv= (ImageView) adapterView.getChildAt(6).findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_write);
                iv= (ImageView) adapterView.getChildAt(7).findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_write);
                break;
            case 1:
                iv= (ImageView) view.findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_2);
                iv= (ImageView) adapterView.getChildAt(0).findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_write);
                iv= (ImageView) adapterView.getChildAt(2).findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_write);
                iv= (ImageView) adapterView.getChildAt(3).findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_write);
                iv= (ImageView) adapterView.getChildAt(4).findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_write);
                iv= (ImageView) adapterView.getChildAt(5).findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_write);
                iv= (ImageView) adapterView.getChildAt(6).findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_write);
                iv= (ImageView) adapterView.getChildAt(7).findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_write);
                break;
            case 2:
                iv= (ImageView) view.findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_3);
                iv= (ImageView) adapterView.getChildAt(1).findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_write);
                iv= (ImageView) adapterView.getChildAt(0).findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_write);
                iv= (ImageView) adapterView.getChildAt(3).findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_write);
                iv= (ImageView) adapterView.getChildAt(4).findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_write);
                iv= (ImageView) adapterView.getChildAt(5).findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_write);
                iv= (ImageView) adapterView.getChildAt(6).findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_write);
                iv= (ImageView) adapterView.getChildAt(7).findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_write);
                break;
            case 3:
                iv= (ImageView) view.findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_4);
                iv= (ImageView) adapterView.getChildAt(1).findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_write);
                iv= (ImageView) adapterView.getChildAt(2).findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_write);
                iv= (ImageView) adapterView.getChildAt(0).findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_write);
                iv= (ImageView) adapterView.getChildAt(4).findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_write);
                iv= (ImageView) adapterView.getChildAt(5).findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_write);
                iv= (ImageView) adapterView.getChildAt(6).findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_write);
                iv= (ImageView) adapterView.getChildAt(7).findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_write);
                break;
            case 4:
                iv= (ImageView) view.findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_5);
                iv= (ImageView) adapterView.getChildAt(1).findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_write);
                iv= (ImageView) adapterView.getChildAt(2).findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_write);
                iv= (ImageView) adapterView.getChildAt(3).findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_write);
                iv= (ImageView) adapterView.getChildAt(0).findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_write);
                iv= (ImageView) adapterView.getChildAt(5).findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_write);
                iv= (ImageView) adapterView.getChildAt(6).findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_write);
                iv= (ImageView) adapterView.getChildAt(7).findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_write);
                break;
            case 5:
                iv= (ImageView) view.findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_6);
                iv= (ImageView) adapterView.getChildAt(1).findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_write);
                iv= (ImageView) adapterView.getChildAt(2).findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_write);
                iv= (ImageView) adapterView.getChildAt(3).findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_write);
                iv= (ImageView) adapterView.getChildAt(4).findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_write);
                iv= (ImageView) adapterView.getChildAt(0).findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_write);
                iv= (ImageView) adapterView.getChildAt(6).findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_write);
                iv= (ImageView) adapterView.getChildAt(7).findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_write);
                break;
            case 6:
                iv= (ImageView) view.findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_7);
                iv= (ImageView) adapterView.getChildAt(1).findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_write);
                iv= (ImageView) adapterView.getChildAt(2).findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_write);
                iv= (ImageView) adapterView.getChildAt(3).findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_write);
                iv= (ImageView) adapterView.getChildAt(4).findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_write);
                iv= (ImageView) adapterView.getChildAt(5).findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_write);
                iv= (ImageView) adapterView.getChildAt(0).findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_write);
                iv= (ImageView) adapterView.getChildAt(7).findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_write);
                break;
            case 7:
                iv= (ImageView) view.findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_blue);
                iv= (ImageView) adapterView.getChildAt(1).findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_write);
                iv= (ImageView) adapterView.getChildAt(2).findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_write);
                iv= (ImageView) adapterView.getChildAt(3).findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_write);
                iv= (ImageView) adapterView.getChildAt(4).findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_write);
                iv= (ImageView) adapterView.getChildAt(5).findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_write);
                iv= (ImageView) adapterView.getChildAt(6).findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_write);
                iv= (ImageView) adapterView.getChildAt(0).findViewById(R.id.item_txview);
                iv.setImageResource(R.drawable.ok_write);
                break;
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        MainActivity.now=this;
        File file=new File("/data/data/com.example.andylau96.res/files/color.txt");
        try {
            InputStreamReader reader=new InputStreamReader(new FileInputStream(file),"UTF-8");
            BufferedReader br=new BufferedReader(reader);
            theme="";
            String temp=null;
            while((temp=br.readLine())!=null)
            {
                theme+=temp;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        title.setBackgroundColor(Color.parseColor(theme));
    }
}
